package com.yami.shop.platform.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.bean.event.SendPersonMessageEvent;
import com.yami.shop.bean.model.Blog;
import com.yami.shop.bean.model.MeltCloudGroup;
import com.yami.shop.bean.param.NotifyTemplateParam;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.handler.SensitiveHandler;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.service.BlogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component
@AllArgsConstructor
public class BlogTask {
    
    private final BlogService blogService;

    private final SensitiveHandler sensitiveHandler;

    private final ApplicationContext applicationContext;

    private final String autoKey = "blogAutoReviewTask";

    /**
     * 自动审核博客 
     * 每分钟执行一次
     */
    @XxlJob("autoReviewBlogs")
    public void autoReviewBlogs() {
        if (RedisUtil.hasKey(autoKey)) {
            log.error("博客自动审核正在进行中……");
            return;
        }
        List<Blog> list = blogService.list(Wrappers.lambdaQuery(Blog.class).eq(Blog::getStatus, 3).eq(Blog::getAuditType, 1));
        if (CollUtil.isEmpty(list)) {
            log.error("未查询到需要自动审核的博客！");
            return;
        }
        RedisUtil.set(autoKey, "true", 0);
        for (Blog blog : list) {
            boolean sensitive = sensitiveHandler.isSensitive(blog.getContent());
            if (sensitive) {
                blog.setStatus(2);
                blog.setReason("发布内容含有敏感信息。");
                if (blog.getArticleSource().equals(1)) {
                    NotifyTemplateParam shopParam = new NotifyTemplateParam();
                    shopParam.setShopId(Constant.PLATFORM_SHOP_ID);
                    shopParam.setSendType(SendType.BLOG_REJECT.getValue());
                    shopParam.setUserId(blog.getUserUid());
                    shopParam.setTitle(blog.getReason());
                    shopParam.setContent(blog.getContent());
                    if (blog.getType().equals(0)) {
                        shopParam.setToTypeName("广场");
                    } else if (blog.getType().equals(2)) {
                        shopParam.setToTypeName("圈子");
                    }
                    applicationContext.publishEvent(new SendPersonMessageEvent(shopParam));
                }
            } else {
                blog.setStatus(1);
            }
        }
        blogService.updateBatchById(list);
        log.info("======成功自动审核博客{}个======", list.size());
        RedisUtil.del(autoKey);
    }
    
    
    
    
    
    
    
}
